import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import booroo.dao.GalleryDAO;
import booroo.dao.GalleryDAO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;


public class ProcessImage extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField regName;
	private JTextField showName;
	JLabel showImg = new JLabel("");

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ProcessImage dialog = new ProcessImage();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ProcessImage() {
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel regPhoto = new JPanel();
			regPhoto.setPreferredSize(new Dimension(250, 10));
			contentPanel.add(regPhoto, BorderLayout.WEST);
			
			final JLabel regImg = new JLabel("");
			regImg.setBorder(new LineBorder(new Color(0, 0, 0)));
			regImg.setPreferredSize(new Dimension(200, 150));
			
			JButton regBtn = new JButton("사진등록");
			regBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(regName.getText().isEmpty()){
						JOptionPane.showMessageDialog(null, "사진등록시 아이디를 입력하세요!");
						return;
					}
					// 이미지만 처리하기 위해 이미지 파일 확장자를 필터로 등록
					FileNameExtensionFilter filter = new FileNameExtensionFilter("이미지 파일들","png","jpg","gif");
					// 사용자가 이미지를 선택할수 있도록 파일열기 대화상자 객체 생성
					JFileChooser fc = new JFileChooser();
					// 대화상자에 필터등록
					fc.setFileFilter(filter);
					// 파일열기 대화상자 화면에 띄우고 선택한 작업내용을 choice에 저장
					int choice = fc.showOpenDialog(ProcessImage.this);
					// 대화상자에서 이미지를 선택하고 열기버튼을 클릭했다면
					if(choice == JFileChooser.APPROVE_OPTION){
						// 선택한 이미지의 전체 경로를 알아냄
						String img = fc.getSelectedFile().getPath();
						// 선택한 이미지를 특정장소에 저장하기 위해 저장위치를 경로로 지정
						String myPhoto = fc.getSelectedFile().getName();
						String savePath ="c:/java/myPhoto/ " + myPhoto;
						// 선택한 이미지를 지정한 경로에 생성하기 위해 BufferedImage객체 이용
						BufferedImage image = null;
						try{
							// 원본 이미지를 File객체로 읽어옴
							File origin = new File(img);
							// File객체로 지정한 파일을 ImageIO객체로 읽어옴
							image = ImageIO.read(origin);
							// 메모리로 읽어온 이미지 내용을 지정한 위치에 지정한 형식으로 저장
							ImageIO.write(image, "png", new File(savePath));
						}catch(Exception ex){ex.printStackTrace();}
						// Galleryv1테이블에 등록한 이미지 저장
						if(GalleryDAO.insertPhoto(regName.getText(),myPhoto)>0)
							JOptionPane.showMessageDialog(null, "이미지 등록 완료!");
						// JLable에 이미지 출력
						regImg.setIcon(new ImageIcon(savePath));
					}
				}
			});
			
			regName = new JTextField();
			regName.setColumns(10);
			GroupLayout gl_regPhoto = new GroupLayout(regPhoto);
			gl_regPhoto.setHorizontalGroup(
				gl_regPhoto.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_regPhoto.createSequentialGroup()
						.addGroup(gl_regPhoto.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_regPhoto.createSequentialGroup()
								.addContainerGap()
								.addComponent(regImg, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE))
							.addGroup(gl_regPhoto.createSequentialGroup()
								.addGap(74)
								.addComponent(regBtn))
							.addGroup(gl_regPhoto.createSequentialGroup()
								.addGap(65)
								.addComponent(regName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addContainerGap())
			);
			gl_regPhoto.setVerticalGroup(
				gl_regPhoto.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_regPhoto.createSequentialGroup()
						.addContainerGap()
						.addComponent(regImg, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addComponent(regName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(8)
						.addComponent(regBtn)
						.addContainerGap(62, Short.MAX_VALUE))
			);
			regPhoto.setLayout(gl_regPhoto);
		}
		{
			JPanel showPhoto = new JPanel();
			showPhoto.setPreferredSize(new Dimension(250, 10));
			contentPanel.add(showPhoto, BorderLayout.EAST);
			
			JButton showBtn = new JButton("사진보기");
			showBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(showName.getText().isEmpty()){
						JOptionPane.showMessageDialog(null, "사진보기시 아이디를 입력하세요!");
						return;
					}
					String imgPath = "c:/java/myPhoto/" + GalleryDAO.selectOnePhoto(showName.getText());
					showImg.setIcon(new ImageIcon(imgPath));
				}
			});
			
			
			showImg.setPreferredSize(new Dimension(200, 150));
			showImg.setBorder(new LineBorder(new Color(0, 0, 0)));
			
			showName = new JTextField();
			showName.setColumns(10);
			GroupLayout gl_showPhoto = new GroupLayout(showPhoto);
			gl_showPhoto.setHorizontalGroup(
				gl_showPhoto.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_showPhoto.createSequentialGroup()
						.addGroup(gl_showPhoto.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_showPhoto.createSequentialGroup()
								.addContainerGap()
								.addComponent(showImg, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_showPhoto.createSequentialGroup()
								.addGap(63)
								.addComponent(showName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGroup(gl_showPhoto.createSequentialGroup()
								.addGap(83)
								.addComponent(showBtn)))
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			);
			gl_showPhoto.setVerticalGroup(
				gl_showPhoto.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_showPhoto.createSequentialGroup()
						.addContainerGap()
						.addComponent(showImg, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
						.addGap(15)
						.addComponent(showName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(showBtn)
						.addContainerGap(67, Short.MAX_VALUE))
			);
			showPhoto.setLayout(gl_showPhoto);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
